from django.contrib.auth.models import AbstractUser
from django.db.models import CharField, DateTimeField, TextChoices
from django.utils import timezone
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.db import models
from django.contrib.auth.models import Group
from datetime import timedelta


class User(AbstractUser):
    """
    Default custom user model for backend.
    If adding fields that need to be filled at user signup,
    check forms.SignupForm and forms.SocialSignupForms accordingly.
    """

    class RoleType(TextChoices):
        TESTER = "Tester"
        CHECKER = "Checker"
        ENDORSER = "Endorser"

    staffNo = CharField(max_length=255, default="")
    name = CharField(_("Name of User"), blank=True, max_length=255)
    enable_email = models.BooleanField(default=True)
    # role = CharField(choices=RoleType.choices, default=RoleType.TESTER, max_length=255)
    # username = CharField(max_length=255, default="")
    # password = CharField(max_length=255, default="")
    # update = DateTimeField(default=timezone.now)

    def get_absolute_url(self) -> str:
        """Get URL for user's detail view.

        Returns:
            str: URL for user detail.

        """
        return reverse("users:detail", kwargs={"username": self.username})


def get_default_datetime():
    return timezone.now() + timedelta(days=30)

class GroupSetting(models.Model):
    group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name="group")
    enable = models.BooleanField(default=True)
    expiry_date = models.DateField(default=get_default_datetime)
    users = models.ForeignKey(User,on_delete=models.CASCADE)

    class Meta:
        unique_together = ["users", "group"]
        permissions = [
            ["can_do_maintenance_task", "Can do maintenance task"],
            ["can_do_maintenance_by_stock_code", "Can do maintenance by stock code"]
        ]

    @property
    def groupname(self):
        return Group.objects.get(pk=self.group.id).name
    
    @property
    def username(self):
        return User.objects.get(pk=self.users.id).username

    def __str__(self):
        return self.group.name
    
